package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.StrategyCatalog;
import cn.wolfcode.trip.base.query.StrategyCatalogQueryObject;
import cn.wolfcode.trip.base.service.IStrategyCatalogService;
import cn.wolfcode.trip.base.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Controller
@RequestMapping("strategyCatalog")
public class StrategyCatalogController {

    @Autowired
    private IStrategyCatalogService strategyCatalogService;

    @RequestMapping("list")
    public String list(Model model, @ModelAttribute("qo") StrategyCatalogQueryObject qo) {
        model.addAttribute("result", strategyCatalogService.queryForList(qo));
        return "strategy/catalog";
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JSONResult saveOrUpdate(StrategyCatalog strategyCatalog) {
        JSONResult result = new JSONResult();
        try {
            if (strategyCatalog.getId() != null) {
                strategyCatalogService.update(strategyCatalog);
            } else {
                strategyCatalogService.save(strategyCatalog);
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    @RequestMapping("listByStrategyId")
    @ResponseBody
    public List<StrategyCatalog> listByStrategyId(Long id) {
        return strategyCatalogService.listByStrategyId(id);
    }


}
